﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TCKQKL.FrontEnd.DataModel;
using System.Collections.Generic;
using TCKQKL.Service.LookupClient;

namespace TCKQKL.FrontEnd.Web.Controls
{
    public partial class OrderList : System.Web.UI.UserControl
    {
        public UpdatePanelUpdateMode UpdateMode
        {
            set { this.pnlOrderList.UpdateMode = value; }
            get { return this.pnlOrderList.UpdateMode; }
        }

        public List<OrdersOnlineUI> orderList = null;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void Update()
        {
            rptOrders.DataSource = orderList;
            rptOrders.DataBind();
            this.pnlOrderList.Update();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string temp = "";
            int result = 0;
            LSClient client = new LSClient();
            foreach (DataListItem item in rptOrders.Items)
            {
                CheckBox chkCancel = (CheckBox)item.FindControl("matched");
                if (chkCancel.Checked)
                {
                    try
                    {
                        string accNo = HttpContext.Current.User.Identity.Name;
                        string symbol = ((Label)item.FindControl("lblSymbol")).Text;
                        string orderSide = ((Label)item.FindControl("lblOrderSide")).Text;
                        string orderClass = ((Label)item.FindControl("lblOrderClass")).Text;
                        string orderSpec = "NORMAL";
                        int qtty = Convert.ToInt32(((Label)item.FindControl("lblQuantity")).Text);
                        string price = ((Label)item.FindControl("lblPrice")).Text;
                        string orderMode = "INTERNET";
                        result = client.CancelOrder(new OrdersOnlineUI(0,
                                                    accNo, orderSide, symbol, orderSpec, orderClass,
                                                    orderMode, DateTime.Now, DateTime.Today, "",
                                                    DateTime.Now, price, qtty, "CANCEL"));
                        client.Close();
                    }
                    catch
                    {
                        Response.Redirect("./Error.aspx");
                    }
                    if (result != 0)
                        Response.Redirect("./Error.aspx?Message=" + result.ToString(), true);
                    else
                        Response.Redirect("./Order.aspx");
                }
            }
        }

        protected void tmRefreshList_Tick(object sender, EventArgs e)
        {
            rptOrders.DataSource = orderList;
            rptOrders.DataBind();
            this.pnlOrderList.Update();
        }

        protected void rptOrders_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Change")
            {
                //CustomerDataList.SelectedIndex = e.Item.ItemIndex;
                //e.CommandArgument;
                LSClient client = new LSClient();
                OrdersOnlineUI selected = client.GetOrderByOrderId(Convert.ToInt32(e.CommandArgument));
                Order o = new Order();
                o.UpdateSelectedOrder(selected);
                
            }
            else
                if (e.CommandName == "summary")
                {
                    //CustomerDataList.SelectedIndex = -1;
                }
        }
    }
}